package com.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.content.model.po.CourseTeacher;
import com.content.service.CourseTeacherService;
import com.content.service.mapper.CourseTeacherMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CourseTeacherServiceImpl extends ServiceImpl<CourseTeacherMapper, CourseTeacher> implements CourseTeacherService {

    /**
     * 新增或修改课程师资信息
     *
     * @param courseTeacher
     */
    @Override
    @Transactional
    public CourseTeacher addCourseTeacher(CourseTeacher courseTeacher) {
        courseTeacher.setCreateDate(LocalDateTime.now());
        List<CourseTeacher> list = lambdaQuery().eq(CourseTeacher::getId, courseTeacher.getId()).list();

        list.forEach(
                item->{
                    if(item.getId().equals(courseTeacher.getId())){
                        update(courseTeacher, new LambdaUpdateWrapper<CourseTeacher>().eq(CourseTeacher::getId, courseTeacher.getId()));
                    }else {
                        save(courseTeacher);
                    }
                }
        );
        if (list.isEmpty()) {
            save(courseTeacher);
        }
        CourseTeacher one = getOne(new LambdaUpdateWrapper<CourseTeacher>().eq(CourseTeacher::getId, courseTeacher.getId()));
        return one;
    }
}
